//
//  COLoginHTTP.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/28.
//

import UIKit
import Moya

let loginProvider = MoyaProvider<COLoginHTTP>()
enum COLoginHTTP {
    case signup(email: String, password: String, nickName: String)
    case logout
    case signin(email: String, password: String)
    case mine
    case complete(nickName: String? = nil, sculpture: String? = nil, age: Int? = nil, level: String? = nil, food: String? = nil)
    case balance
    case apple(code: String)
    case userLogin(loginCode: String)
}

extension COLoginHTTP: TargetType {
    var path: String {
        switch self {
        case .signup:
            return "/coco/coco589/register"
        case .logout:
            return "/coco/coco589/login/out"
        case .signin:
            return "/coco/coco589/login"
        case .mine:
            return "/coco/coco589/user/my"
        case .complete:
            return "/coco/coco589/user/9090/edit"
        case .balance:
            return "/coco/coco589/user/888898"
        case .apple:
            return "/coco/coco589"
        case .userLogin:
            return "/coco/coco589/guest"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .signup:
            return .post
        case .logout:
            return .get
        case .signin:
            return .post
        case .mine:
            return .get
        case .complete:
            return .post
        case .balance:
            return .get
        case .apple:
            return .post
        case .userLogin:
            return .post
        }
    }
    
    var task: Moya.Task {
        var params: [String: Any] = [:]
        switch self {
        case let .signup(email, password, nickName):
            params["email"] = email
            params["password"] = password
            params["nickName"] = nickName
        case .logout:
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        case let .signin(email, password):
            params["email"] = email
            params["password"] = password
        case .mine:
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        case let .complete(nickName, sculpture, age, level, food):
            if let nickName = nickName, !nickName.isEmpty {
                params["nickName"] = nickName
            }
            if let sculpture = sculpture, !sculpture.isEmpty {
                params["sculpture"] = sculpture
            }
            if let age = age {
                params["age"] = age
            }
            params["level"] = level
            if let food = food, !food.isEmpty {
                params["food"] = food
            }
        case .balance:
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        case .apple(let code):
            params["loginCode"] = code
        case let .userLogin(loginCode):
            params["loginCode"] = loginCode
        }
        
        return .requestParameters(parameters: params, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        var params: [String: String] = [:]
        params["AVS"] = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
        params["OSB"] = "1"
        params["AGKB"] = "H1"
        params["token"] = COUserManager.userInfo?.token
        return params
    }
}

